package com.example.common.tools

import java.io.Serializable

class AcacheUtil {

    companion object {
        val instance by lazy(LazyThreadSafetyMode.SYNCHRONIZED) {
            AcacheUtil()
        }
    }

    private val cache = ACache.get(TooldUtils.getApplication())

    fun putString(key: String, value: String) {
        cache.put(key, value)
    }

    fun getString(key: String): String? {
        return cache.getAsString(key)
    }

    fun putSerializ(key: String, value: Serializable) {
        cache.put(key, value)
    }

    fun <T> getSerializ(key: String): T {
        return cache.getAsObject(key) as T
    }

    fun putJsonArray(key: String, list: List<*>) {
        var array = JsonUtil.instance.toJsonArray(list)
        LogUtils.eLog("Acache", "putJsonArray = $array")
        cache.put(key, array.toString())
    }

    fun <T> getJsonArray(key: String, clazz: Class<T>): ArrayList<T> {
        var json = cache.getAsString(key)
        LogUtils.eLog("Acache", "getJsonArray = $json")
        return JsonUtil.instance.toList(json, clazz) as ArrayList<T>
    }

    fun remove(key: String) {
        cache.remove(key)
    }
}

